/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.features;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import org.osdtsystem.matrixparser.features.keys.StringListFactory;
import org.osdtsystem.matrixparser.logging.Log;

/**
 *
 * @author Martin Haulrich
 */
public class SparseBinaryFeatureVector extends AbstractFeatureVector {

    Set<Feature> features;

    public SparseBinaryFeatureVector() {
        features = new HashSet<Feature>();
    }

    public double getValue(Feature f) {
        if (features.contains(f)) {
            return 1;
        }
        return 0d;
    }

    Feature[] keys() {
        Feature[] keys = features.toArray(new Feature[0]);
        Arrays.sort(keys);
        return keys;
    }


    public int checksum() {
        int checksum = 13;
        for (Feature key : keys()) {
            checksum = 19 * checksum + key.id();
        }
        return checksum;
    }

    public String checksum64() {
        return Log.base64(checksum());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder().append("SparseBinaryFeatureVector: checksum=").append(checksum64()).append(" ");
        String sep = "";
        int count = 0;
        for (Feature key : keys()) {
            if (count++ % 20 == 0)
                sb.append("\n\t");
            sb.append("" + key.id()).append(sep);
            sep = " ";
            if (count > 100) {
                sb.append("\n\t...");
                break;
            }
        }
        sb.append("\n");
        return sb.toString();
    }


    /**
     * Feature with id zero is 'unknown feature' do not add this
     * @param f
     * @param v
     */
    public void setValue(Feature f, double v) {
        if (f.id() > 0) {
            features.add(f);
        }
    }

    public void addValue(Feature f, double v) {
        if (f.id() > 0 && Double.compare(v, 0) != 0) {
            features.add(f);
        }
    }

    public Collection<Feature> allEntries() {
        return features;
    }

    public int size() {
        return features.size();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof FeatureVector))
            return false;
        if (obj instanceof SparseBinaryFeatureVector)
            return features.equals(((SparseBinaryFeatureVector) obj).features);
        FeatureVector other = (FeatureVector) obj;
        for (Feature f : features) {
            if (Double.compare(other.getValue(f), 1) != 0) {
                return false;
            }
        }

        for (Entry<Feature, Double> entry : other) {
            if (Double.compare(getValue(entry.getKey()), entry.getValue()) != 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 41 * hash + (this.features != null ? this.features.hashCode() : 0);
        return hash;
    }

    @Override
    public Iterator<Entry<Feature, Double>> iterator() {
        return (new SparseBinaryFeatureVectorIterator());
    }

    public void addValue(FeatureHandler fh, CharSequence chars, double v) {
        //addValue(fh.getFeature(chars.toString()), v);
        addValue(fh.getFeature(StringListFactory.combine(chars.toString())), v);
    }

    public class SparseBinaryFeatureVectorIterator
            implements Iterator<Entry<Feature, Double>> {
        Iterator<Feature> featureIterator;
        FeatureValue featureValue = new FeatureValue(null, 1);

        public SparseBinaryFeatureVectorIterator() {
            featureIterator = features.iterator();
        }

        public boolean hasNext() {
            return featureIterator.hasNext();
        }

        public Entry<Feature, Double> next() {
            Feature f = featureIterator.next();
            featureValue.feature = f;
            return (featureValue);
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }
    }

    public void ignoreFeatures(FeatureVector fv) {
        for (Entry<Feature,Double> entry : this) {
            Feature key = entry.getKey();
            if (Double.compare(fv.getValue(key), 0) != 0)
                features.add(key);
        }
    }

    public void add(FeatureVector fv) {
        for (Entry<Feature,Double> entry : fv) {
            if (Double.compare(entry.getValue(), 0) != 0) {
                features.add(entry.getKey());
            }
        }
    }
}

